package com.zed.infrastructure.persistence.mybatis.plus.core;

import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.exception.FlexAssert;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.core.mybatis.FlexSqlSessionFactoryBuilder;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;

/**
 * 自定义的{@link MybatisFlexBootstrap}
 *
 * @author Zed
 */
public class CustomMybatisFlexBootstrap extends MybatisFlexBootstrap {

    @Override
    public CustomMybatisFlexBootstrap start() {
        if (started.compareAndSet(false, true)) {

            FlexAssert.notNull(dataSource,"dataSource");

            //init configuration
            if (configuration == null) {

                if (transactionFactory == null) {
                    transactionFactory = new JdbcTransactionFactory();
                }

                Environment environment = new Environment(environmentId, transactionFactory, dataSource);
                configuration = new FlexConfiguration(environment);
            }

            if (logImpl != null) {
                configuration.setLogImpl(logImpl);
            }

            //init sqlSessionFactory
            new CustomFlexSqlSessionFactoryBuilder().build(configuration);

            //init mappers
            if (mappers != null) {
                mappers.forEach(configuration::addMapper);
            }

            LogFactory.getLog(MybatisFlexBootstrap.class).debug("Mybatis-Flex has started.");
        }

        return this;
    }

    @Override
    public <T> CustomMybatisFlexBootstrap addMapper(Class<T> type) {
        super.addMapper(type);
        return this;
    }

    @Override
    public CustomMybatisFlexBootstrap setEnvironmentId(String environmentId) {
        super.setEnvironmentId(environmentId);
        return this;
    }

    @Override
    public CustomMybatisFlexBootstrap setTransactionFactory(TransactionFactory transactionFactory) {
        super.setTransactionFactory(transactionFactory);
        return this;
    }

    @Override
    public CustomMybatisFlexBootstrap setDataSource(String dataSourceKey, DataSource dataSource) {
        super.setDataSource(dataSourceKey, dataSource);
        return this;
    }

    @Override
    public CustomMybatisFlexBootstrap addDataSource(String dataSourceKey, DataSource dataSource) {
        super.addDataSource(dataSourceKey, dataSource);
        return this;
    }

    @Override
    public CustomMybatisFlexBootstrap setConfiguration(FlexConfiguration configuration) {
        super.setConfiguration(configuration);
        return this;
    }

    @Override
    public CustomMybatisFlexBootstrap setLogImpl(Class<? extends Log> logImpl) {
        super.setLogImpl(logImpl);
        return this;
    }
}
